"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : gs_om -t cert分发证书成功不指定-l（主备）
Create At   : 2022/06/10
Owner       : opentestcase023
Description :
    1.拷贝normal.zip至cluster文件夹下
    2.执行证书分发操作
    gs_om -t cert --cert-file normal.zip
    3.清理环境
Expect      :
    1.文件传输成功
    2.证书分发正常
    3.环境清理成功
History     :
    modified by opentestcase012 at 2022-10-3:拷贝文件的执行用户修改为数据库用户,避免权限问题执行失败
    modified by opentestcase012 at 2022-11-22:代码变更，优化断言
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Logger import Logger

COMMONSH = CommonSH("PrimaryDbUser")


@unittest.skipIf(3 != COMMONSH.get_node_num(), '非1+2环境不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_root_node = Node('PrimaryRoot')
        self.primary_node = Node('PrimaryDbUser')
        self.u_name = self.primary_node.ssh_user
        self.log_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                     'cert.log')
        self.cert_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                      'cert_file')
        self.cert_file = os.path.join(self.cert_path, 'normal.zip')

    def test_gs_om(self):
        text = '----step1:传输normal.zip文件至路径 expect:成功----'
        self.log.info(text)
        self.common.scp_file(self.primary_node, 'normal.zip',
                             self.cert_path)

        text = '----step2:执行证书分发操作 expect:成功----'
        self.log.info(text)
        cert_cmd = f'source {macro.DB_ENV_PATH};' \
                   f'gs_om -t cert --cert-file {self.cert_file}'
        self.log.info(cert_cmd)
        cert_res = self.primary_node.sh(cert_cmd).result()
        self.log.info(cert_res)
        check_msg = f'Successfully distributed cert files on all nodes'
        self.assertIn(check_msg, cert_res, '证书分发失败')

    def tearDown(self):
        text = "----step3：恢复环境 expect:成功----"
        self.log.info(text)
        cert_cmd = f'source {macro.DB_ENV_PATH};' \
                   f'gs_om -t cert -l {self.log_path} --rollback'
        self.log.info(cert_cmd)
        cert_res = self.primary_node.sh(cert_cmd).result()
        self.log.info(cert_res)
        check_cmd = f'cd {os.path.dirname(self.log_path)};' \
                    f'ls {os.path.dirname(self.log_path)} | ' \
                    f'grep cert*.log | xargs cat'
        self.log.info(check_cmd)
        log_check = self.primary_root_node.sh(check_cmd).result()
        self.log.info(log_check)
        del_cmd = f'rm -rf {self.cert_path};rm -rf {self.log_path}'
        del_res = self.primary_root_node.sh(del_cmd).result()
        self.log.info(del_res)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
        self.assertEqual(3, cert_res.count('Successfully rollback'),
                         '证书回滚失败')
        self.assertEqual('', del_res, '文件删除失败')
